package org.example.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.example.pojo.Result;
import org.example.pojo.User;
import org.example.properties.JwtProperties;
import org.example.utils.JwtUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class LoginController {

    @Resource
    JwtProperties jwtProperties;
    @PostMapping("/user/login")
    public Result login( User user, HttpServletRequest request){
        log.info("当前user:{}",user);
        String authorization = request.getHeader("Authorization");
        log.info("token:{}",authorization);

        Map<String,Object> map = new HashMap<>();
        map.put(user.getUsername(),user.getUsername());
        map.put(user.getPassword(),user.getPassword());
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), map);

        log.info("生成token:{}",token);
        return Result.success(token);
    }
    @GetMapping("/user")
    public Result t(){
        return Result.success("success!");
    }

}
